<!doctype html>
<html lang="zh-cn">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>golang中的生产消费模型 | lsy&#39;blog</title>
    <meta property="og:title" content="golang中的生产消费模型 - lsy&#39;blog">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2023-10-31T17:36:47&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2023-10-31T17:36:47&#43;08:00'>
        
    <meta name="Keywords" content="golang,go语言,go语言笔记,lsy,java,博客,软件架构">
    <meta name="description" content="golang中的生产消费模型">
        
    <meta name="author" content="lsy">
    <meta property="og:url" content="http://kklt1996.gitee.io/blog/post/golang/2023-10-31-go%E7%94%9F%E4%BA%A7%E6%B6%88%E8%B4%B9%E6%A8%A1%E5%9E%8B/">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">

    <link rel="stylesheet" href='/blog/css/normalize.css'>
    <link rel="stylesheet" href='/blog/css/style.css'>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    


    
    
        <link rel="stylesheet" href='/css/douban.css'>
    
        <link rel="stylesheet" href='/css/other.css'>
    
</head>


<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="http://kklt1996.gitee.io/blog/">
                        lsy&#39;blog
                    </a>
                
                <p class="description">专注于Java、Go语言(golang)、软件架构</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="http://kklt1996.gitee.io/blog/">首页</a>
                    
                    <a  href="http://kklt1996.gitee.io/blog/archives/" title="归档">归档</a>
                    
                    <a  href="http://kklt1996.gitee.io/blog/categories/" title="分类">分类</a>
                    
                    <a  href="http://kklt1996.gitee.io/blog/tags/" title="标签">标签</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#单一生产者单channel单一消费者">单一生产者，单channel，单一消费者</a>
      <ul>
        <li></li>
      </ul>
    </li>
    <li><a href="#单一生产者单channel多消费者">单一生产者，单channel，多消费者</a>
      <ul>
        <li></li>
      </ul>
    </li>
    <li><a href="#单一生产者多channel多消费者">单一生产者，多channel，多消费者</a>
      <ul>
        <li></li>
      </ul>
    </li>
    <li><a href="#单一生产者多channel单一消费者">单一生产者，多channel，单一消费者</a>
      <ul>
        <li></li>
      </ul>
    </li>
    <li><a href="#多生产者单channel单消费者">多生产者，单channel，单消费者</a>
      <ul>
        <li></li>
      </ul>
    </li>
    <li><a href="#多生产者单channel多消费者">多生产者，单channel，多消费者</a>
      <ul>
        <li></li>
      </ul>
    </li>
    <li><a href="#多生产者多channel单一消费者">多生产者，多channel，单一消费者</a>
      <ul>
        <li></li>
      </ul>
    </li>
    <li><a href="#多生产者多channel多消费者">多生产者，多channel，多消费者</a>
      <ul>
        <li></li>
      </ul>
    </li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">golang中的生产消费模型</h1>
        </header>
        <date class="post-meta meta-date">
            2023年10月31日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='http://kklt1996.gitee.io/categories/golang'>golang</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="clear" style="display: none">
            <div class="toc-article">
                <div class="toc-title">文章目录</div>
            </div>
        </div>
        
        <div class="post-content">
            <p>golang语言实现生产消费模型</p>
<h2 id="单一生产者单channel单一消费者">单一生产者，单channel，单一消费者</h2>
<p>适用于吞吐量排序: 生产者&lt;消费者&lt;channel</p>
<h4 id="生产消费模型">生产消费模型</h4>
<pre tabindex="0"><code class="language-mermaid" data-lang="mermaid">graph LR;

    生产者--&gt;channel;
    channel--&gt;消费者;
</code></pre><h4 id="程序关闭时的时序图说明如何保证优雅停止消费和已经产出的消息处理完成">程序关闭时的时序图说明如何保证优雅停止消费和已经产出的消息处理完成</h4>
<pre tabindex="0"><code class="language-mermaid" data-lang="mermaid">sequenceDiagram
    participant 操作系统
    participant go应用程序监听signal的协程
    participant go应用程序生产者的协程
    participant go应用程序消费者的协程
    participant go应用程序主协程
    操作系统-&gt;&gt;go应用程序监听signal的协程: kill signal
    go应用程序监听signal的协程-&gt;&gt;go应用程序生产者的协程: 通知停止生产
    go应用程序生产者的协程-&gt;&gt;go应用程序消费者的协程: 生产者关闭所持有的channel
    go应用程序消费者的协程-&gt;&gt;go应用程序主协程: 消费任务处理完毕
</code></pre><p>当接受到应用程序kill信号的时候，通知生产者停止生产，然后关闭生产者所拥有的channel。
消费者监听到消费的channel关闭的时候跳出任务处理。主线程阻塞至所有的消费者关闭完成则退出。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 15
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 16
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 17
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 18
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 19
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 20
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 21
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 22
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 23
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 24
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 25
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 26
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 27
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 28
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 29
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 30
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 31
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 32
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 33
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 34
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 35
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 36
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 37
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 38
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 39
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 40
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 41
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 42
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 43
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 44
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 45
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 46
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 47
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 48
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 49
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 50
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 51
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 52
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 53
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 54
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 55
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 56
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 57
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 58
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 59
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 60
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 61
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 62
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 63
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 64
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 65
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 66
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 67
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 68
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 69
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 70
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 71
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 72
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 73
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 74
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 75
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 76
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 77
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 78
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 79
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 80
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 81
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 82
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 83
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 84
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 85
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 86
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 87
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 88
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 89
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 90
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 91
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 92
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 93
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 94
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 95
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 96
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 97
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 98
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 99
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">100
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">101
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">102
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">103
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">104
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">105
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">106
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">107
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">108
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">109
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">110
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">111
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">112
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">113
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">114
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">115
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">116
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">117
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">118
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">119
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">package</span> main
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">import</span> (
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;context&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;log&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;math/rand&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os/signal&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;sync&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;syscall&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;time&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Message <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	id <span style="color:#458;font-weight:bold">int</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Producer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannel <span style="color:#000;font-weight:bold">chan</span><span style="color:#000;font-weight:bold">&lt;-</span> Message
</span></span><span style="display:flex;"><span>	ctx         context.Context
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannel <span style="color:#000;font-weight:bold">chan</span><span style="color:#000;font-weight:bold">&lt;-</span> Message, ctx context.Context) <span style="color:#000;font-weight:bold">*</span>Producer {
</span></span><span style="display:flex;"><span>	p <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Producer{taskChannel: taskChannel,
</span></span><span style="display:flex;"><span>		ctx: ctx}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> p
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (p <span style="color:#000;font-weight:bold">*</span>Producer) <span style="color:#900;font-weight:bold">Produce</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>    random <span style="color:#000;font-weight:bold">:=</span> rand.<span style="color:#900;font-weight:bold">New</span>(rand.<span style="color:#900;font-weight:bold">NewSource</span>(<span style="color:#099">1024</span>))
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#998;font-style:italic">// 判断是否要退出了
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>        <span style="color:#000;font-weight:bold">case</span> <span style="color:#000;font-weight:bold">&lt;-</span>p.ctx.<span style="color:#900;font-weight:bold">Done</span>():
</span></span><span style="display:flex;"><span>            log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s produce break&#34;</span>, name)
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// produce在监听到种终止事件之后关闭channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            <span style="color:#0086b3">close</span>(p.taskChannel)
</span></span><span style="display:flex;"><span>            <span style="color:#000;font-weight:bold">return</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">default</span>:
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 模拟生产者获取数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            num <span style="color:#000;font-weight:bold">:=</span> random.<span style="color:#900;font-weight:bold">Int</span>()
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 间隔一秒产出一个数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            time.<span style="color:#900;font-weight:bold">Sleep</span>(<span style="color:#099">1</span> <span style="color:#000;font-weight:bold">*</span> time.Second)
</span></span><span style="display:flex;"><span>            p.taskChannel <span style="color:#000;font-weight:bold">&lt;-</span> Message{id: num}
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Consumer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannel <span style="color:#000;font-weight:bold">&lt;-</span><span style="color:#000;font-weight:bold">chan</span> Message
</span></span><span style="display:flex;"><span>	wg          <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannel <span style="color:#000;font-weight:bold">&lt;-</span><span style="color:#000;font-weight:bold">chan</span> Message, wg <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup) <span style="color:#000;font-weight:bold">*</span>Consumer {
</span></span><span style="display:flex;"><span>	c <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Consumer{taskChannel: taskChannel,
</span></span><span style="display:flex;"><span>		wg: wg}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> c
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (c <span style="color:#000;font-weight:bold">*</span>Consumer) <span style="color:#900;font-weight:bold">Consume</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">case</span> task, ok <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&lt;-</span>c.taskChannel:
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 模拟消费者处理数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            <span style="color:#000;font-weight:bold">if</span> ok {
</span></span><span style="display:flex;"><span>                log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer  deal: %d&#34;</span>, name, task.id)
</span></span><span style="display:flex;"><span>            } <span style="color:#000;font-weight:bold">else</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#998;font-style:italic">// 数据消费完成，且channel关闭的时候，通知主线程消费者处理管道中的数据完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>                log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer done &#34;</span>, name)
</span></span><span style="display:flex;"><span>                <span style="color:#000;font-weight:bold">goto</span> flag
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#998;font-style:italic">// 跳出多层循环
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>flag:
</span></span><span style="display:flex;"><span>    <span style="color:#998;font-style:italic">// 消费者标识所有消息已经处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>    <span style="color:#000;font-weight:bold">defer</span> c.wg.<span style="color:#900;font-weight:bold">Done</span>()
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main start&#34;</span>)
</span></span><span style="display:flex;"><span>	parent <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">Background</span>()
</span></span><span style="display:flex;"><span>	wc, cancelFunc <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">WithCancel</span>(parent)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>() {
</span></span><span style="display:flex;"><span>		signals <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> os.Signal)
</span></span><span style="display:flex;"><span>		signal.<span style="color:#900;font-weight:bold">Notify</span>(signals, syscall.SIGINT, syscall.SIGTERM)
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 这个协程 就阻塞这里等待结果
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		<span style="color:#000;font-weight:bold">&lt;-</span>signals
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 监听进程kill信号，通知produce停止产出
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;start deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#900;font-weight:bold">cancelFunc</span>()
</span></span><span style="display:flex;"><span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;end deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>	}()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 用于判断所有消费者处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>sync.WaitGroup{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	taskChannel <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> Message)
</span></span><span style="display:flex;"><span>	producer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannel, wc)
</span></span><span style="display:flex;"><span>	consumer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannel, wg)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 一个生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	<span style="color:#000;font-weight:bold">go</span> producer.<span style="color:#900;font-weight:bold">Produce</span>(<span style="color:#d14">&#34;p&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 一个消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg.<span style="color:#900;font-weight:bold">Add</span>(<span style="color:#099">1</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">go</span> consumer.<span style="color:#900;font-weight:bold">Consume</span>(<span style="color:#d14">&#34;c&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 等待consumer完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg.<span style="color:#900;font-weight:bold">Wait</span>()
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main end&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></td></tr></table>
</div>
</div><p>运行程序，终止程序的日志。可以看到关闭时的顺序是生产者停止生产后消费者处理完消息，主协程退出</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>2023/10/31 21:50:26 main start
</span></span><span style="display:flex;"><span>2023/10/31 21:50:27 c consumer  deal: 882526541242338811
</span></span><span style="display:flex;"><span>2023/10/31 21:50:28 c consumer  deal: 6993343009486954757
</span></span><span style="display:flex;"><span>2023/10/31 21:50:29 c consumer  deal: 3359683908485645107
</span></span><span style="display:flex;"><span>2023/10/31 21:50:30 c consumer  deal: 5363318930287979427
</span></span><span style="display:flex;"><span>2023/10/31 21:50:31 start deal kill signal
</span></span><span style="display:flex;"><span>2023/10/31 21:50:31 end deal kill signal
</span></span><span style="display:flex;"><span>2023/10/31 21:50:31 p produce break
</span></span><span style="display:flex;"><span>2023/10/31 21:50:31 c consumer  deal: 7051871835526759457
</span></span><span style="display:flex;"><span>2023/10/31 21:50:31 c consumer done 
</span></span><span style="display:flex;"><span>2023/10/31 21:50:31 main end
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="单一生产者单channel多消费者">单一生产者，单channel，多消费者</h2>
<p>适用于吞吐量排序：消费者&lt;生产者&lt;channel 这个时候增加消费者的数量可以提高吞吐。</p>
<h4 id="生产消费模型-1">生产消费模型</h4>
<pre tabindex="0"><code class="language-mermaid" data-lang="mermaid">graph LR;

    生产者--&gt;channel;
    channel--&gt;消费者1;
    channel--&gt;消费者2;
    channel--&gt;消费者3;
</code></pre><p>代码在单一消费者基础上修改，只需要多个开启多个消费者协程即可</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 15
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 16
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 17
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 18
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 19
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 20
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 21
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 22
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 23
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 24
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 25
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 26
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 27
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 28
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 29
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 30
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 31
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 32
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 33
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 34
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 35
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 36
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 37
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 38
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 39
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 40
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 41
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 42
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 43
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 44
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 45
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 46
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 47
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 48
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 49
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 50
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 51
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 52
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 53
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 54
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 55
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 56
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 57
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 58
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 59
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 60
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 61
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 62
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 63
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 64
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 65
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 66
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 67
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 68
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 69
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 70
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 71
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 72
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 73
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 74
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 75
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 76
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 77
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 78
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 79
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 80
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 81
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 82
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 83
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 84
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 85
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 86
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 87
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 88
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 89
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 90
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 91
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 92
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 93
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 94
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 95
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 96
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 97
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 98
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 99
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">100
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">101
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">102
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">103
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">104
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">105
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">106
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">107
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">108
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">109
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">110
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">111
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">112
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">113
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">114
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">115
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">116
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">117
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">118
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">119
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">120
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">121
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">122
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">123
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">124
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">125
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">126
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">127
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">128
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">package</span> main
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">import</span> (
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;context&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;log&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;math/rand&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os/signal&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;strconv&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;sync&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;syscall&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;time&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Message <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	id <span style="color:#458;font-weight:bold">int</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Producer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannel <span style="color:#000;font-weight:bold">chan</span><span style="color:#000;font-weight:bold">&lt;-</span> Message
</span></span><span style="display:flex;"><span>	ctx         context.Context
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannel <span style="color:#000;font-weight:bold">chan</span><span style="color:#000;font-weight:bold">&lt;-</span> Message, ctx context.Context) <span style="color:#000;font-weight:bold">*</span>Producer {
</span></span><span style="display:flex;"><span>	p <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Producer{taskChannel: taskChannel,
</span></span><span style="display:flex;"><span>		ctx: ctx}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> p
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (p <span style="color:#000;font-weight:bold">*</span>Producer) <span style="color:#900;font-weight:bold">Produce</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>    random <span style="color:#000;font-weight:bold">:=</span> rand.<span style="color:#900;font-weight:bold">New</span>(rand.<span style="color:#900;font-weight:bold">NewSource</span>(<span style="color:#099">1024</span>))
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#998;font-style:italic">// 判断是否要退出了
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>        <span style="color:#000;font-weight:bold">case</span> <span style="color:#000;font-weight:bold">&lt;-</span>p.ctx.<span style="color:#900;font-weight:bold">Done</span>():
</span></span><span style="display:flex;"><span>            log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s produce break&#34;</span>, name)
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// produce在监听到种终止事件之后关闭channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            <span style="color:#0086b3">close</span>(p.taskChannel)
</span></span><span style="display:flex;"><span>            <span style="color:#000;font-weight:bold">return</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">default</span>:
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 模拟生产者获取数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            num <span style="color:#000;font-weight:bold">:=</span> random.<span style="color:#900;font-weight:bold">Int</span>()
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 间隔一秒产出一个数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            time.<span style="color:#900;font-weight:bold">Sleep</span>(<span style="color:#099">1</span> <span style="color:#000;font-weight:bold">*</span> time.Second)
</span></span><span style="display:flex;"><span>            p.taskChannel <span style="color:#000;font-weight:bold">&lt;-</span> Message{id: num}
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Consumer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannel <span style="color:#000;font-weight:bold">&lt;-</span><span style="color:#000;font-weight:bold">chan</span> Message
</span></span><span style="display:flex;"><span>	wg          <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannel <span style="color:#000;font-weight:bold">&lt;-</span><span style="color:#000;font-weight:bold">chan</span> Message, wg <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup) <span style="color:#000;font-weight:bold">*</span>Consumer {
</span></span><span style="display:flex;"><span>	c <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Consumer{taskChannel: taskChannel,
</span></span><span style="display:flex;"><span>		wg: wg}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> c
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (c <span style="color:#000;font-weight:bold">*</span>Consumer) <span style="color:#900;font-weight:bold">Consume</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">case</span> task, ok <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&lt;-</span>c.taskChannel:
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 模拟消费者处理数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            <span style="color:#000;font-weight:bold">if</span> ok {
</span></span><span style="display:flex;"><span>                log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer  deal: %d&#34;</span>, name, task.id)
</span></span><span style="display:flex;"><span>            } <span style="color:#000;font-weight:bold">else</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#998;font-style:italic">// 数据消费完成，且channel关闭的时候，通知主线程消费者处理管道中的数据完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>                log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer done &#34;</span>, name)
</span></span><span style="display:flex;"><span>                <span style="color:#000;font-weight:bold">goto</span> flag
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#998;font-style:italic">// 跳出多层循环
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>flag:
</span></span><span style="display:flex;"><span>    <span style="color:#998;font-style:italic">// 消费者标识所有消息已经处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>    <span style="color:#000;font-weight:bold">defer</span> c.wg.<span style="color:#900;font-weight:bold">Done</span>()
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (c <span style="color:#000;font-weight:bold">*</span>Consumer) <span style="color:#900;font-weight:bold">GoConsume</span>(name <span style="color:#458;font-weight:bold">string</span>, goNum <span style="color:#458;font-weight:bold">int</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">0</span>; i &lt; goNum; i<span style="color:#000;font-weight:bold">++</span> {
</span></span><span style="display:flex;"><span>		c.wg.<span style="color:#900;font-weight:bold">Add</span>(<span style="color:#099">1</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>(goName <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>			c.<span style="color:#900;font-weight:bold">Consume</span>(goName)
</span></span><span style="display:flex;"><span>		}(name <span style="color:#000;font-weight:bold">+</span> <span style="color:#d14">&#34;-&#34;</span> <span style="color:#000;font-weight:bold">+</span> strconv.<span style="color:#900;font-weight:bold">Itoa</span>(i))
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main start&#34;</span>)
</span></span><span style="display:flex;"><span>	parent <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">Background</span>()
</span></span><span style="display:flex;"><span>	wc, cancelFunc <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">WithCancel</span>(parent)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>() {
</span></span><span style="display:flex;"><span>		signals <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> os.Signal)
</span></span><span style="display:flex;"><span>		signal.<span style="color:#900;font-weight:bold">Notify</span>(signals, syscall.SIGINT, syscall.SIGTERM)
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 这个协程 就阻塞这里等待结果
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		<span style="color:#000;font-weight:bold">&lt;-</span>signals
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 监听进程kill信号，通知produce停止产出
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;start deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#900;font-weight:bold">cancelFunc</span>()
</span></span><span style="display:flex;"><span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;end deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>	}()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 用于判断所有消费者处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>sync.WaitGroup{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	taskChannel <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> Message)
</span></span><span style="display:flex;"><span>	producer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannel, wc)
</span></span><span style="display:flex;"><span>	consumer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannel, wg)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 一个生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	<span style="color:#000;font-weight:bold">go</span> producer.<span style="color:#900;font-weight:bold">Produce</span>(<span style="color:#d14">&#34;p&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 一个消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumer.<span style="color:#900;font-weight:bold">GoConsume</span>(<span style="color:#d14">&#34;c&#34;</span>,<span style="color:#099">3</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 等待consumer完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg.<span style="color:#900;font-weight:bold">Wait</span>()
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main end&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></td></tr></table>
</div>
</div><p>同样运行程序，终止程序的日志。可以看到关闭时的顺序是生产者停止生产后消费者处理完消息，主协程退出</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>2023/10/31 21:57:20 main start
</span></span><span style="display:flex;"><span>2023/10/31 21:57:21 c-2 consumer  deal: 882526541242338811
</span></span><span style="display:flex;"><span>2023/10/31 21:57:22 c-0 consumer  deal: 6993343009486954757
</span></span><span style="display:flex;"><span>2023/10/31 21:57:23 c-1 consumer  deal: 3359683908485645107
</span></span><span style="display:flex;"><span>2023/10/31 21:57:24 c-2 consumer  deal: 5363318930287979427
</span></span><span style="display:flex;"><span>2023/10/31 21:57:25 c-0 consumer  deal: 7051871835526759457
</span></span><span style="display:flex;"><span>2023/10/31 21:57:26 c-1 consumer  deal: 6221605816674811576
</span></span><span style="display:flex;"><span>2023/10/31 21:57:26 start deal kill signal
</span></span><span style="display:flex;"><span>2023/10/31 21:57:26 end deal kill signal
</span></span><span style="display:flex;"><span>2023/10/31 21:57:27 p produce break
</span></span><span style="display:flex;"><span>2023/10/31 21:57:27 c-2 consumer  deal: 5938020141159581395
</span></span><span style="display:flex;"><span>2023/10/31 21:57:27 c-2 consumer done 
</span></span><span style="display:flex;"><span>2023/10/31 21:57:27 c-0 consumer done 
</span></span><span style="display:flex;"><span>2023/10/31 21:57:27 c-1 consumer done 
</span></span><span style="display:flex;"><span>2023/10/31 21:57:27 main end
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="单一生产者多channel多消费者">单一生产者，多channel，多消费者</h2>
<ol>
<li>适用于生产者速度大于消费者，且大于单个channel的时候。这时候提高channel的数量解决channel的瓶颈，同时可以提升消费者的数量。 吞吐量：消费者&lt;channel&lt;生产者</li>
<li>适用于生产者速度大于消费者，且需要按照一定规则保证处理顺序的时候。这个时候可以扩展channel数量，按照规则额hash到指定的channel，每个channel
绑定一个协程处理。吞吐量：消费者&lt;生产者&lt;channel</li>
</ol>
<p>实际生产消费模型中一般是第二种情况居多。</p>
<h4 id="生产消费模型-2">生产消费模型</h4>
<pre tabindex="0"><code class="language-mermaid" data-lang="mermaid">graph LR;

    生产者1--&gt;channel1;
    生产者1--&gt;channel2;
    生产者1--&gt;channel3;
    channel1--&gt;消费者1;
    channel2--&gt;消费者2;
    channel3--&gt;消费者3;
</code></pre><p>代码将一个生产者支持绑定多个channel即可，然后将channel分配给消费者。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 15
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 16
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 17
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 18
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 19
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 20
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 21
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 22
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 23
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 24
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 25
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 26
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 27
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 28
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 29
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 30
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 31
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 32
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 33
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 34
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 35
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 36
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 37
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 38
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 39
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 40
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 41
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 42
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 43
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 44
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 45
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 46
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 47
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 48
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 49
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 50
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 51
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 52
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 53
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 54
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 55
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 56
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 57
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 58
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 59
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 60
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 61
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 62
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 63
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 64
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 65
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 66
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 67
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 68
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 69
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 70
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 71
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 72
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 73
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 74
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 75
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 76
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 77
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 78
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 79
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 80
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 81
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 82
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 83
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 84
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 85
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 86
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 87
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 88
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 89
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 90
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 91
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 92
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 93
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 94
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 95
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 96
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 97
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 98
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 99
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">100
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">101
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">102
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">103
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">104
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">105
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">106
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">107
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">108
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">109
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">110
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">111
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">112
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">113
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">114
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">115
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">116
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">117
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">118
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">119
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">120
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">121
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">122
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">123
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">124
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">125
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">126
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">127
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">128
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">129
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">130
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">131
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">132
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">133
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">134
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">135
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">136
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">137
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">138
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">139
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">140
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">141
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">142
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">143
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">144
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">145
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">146
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">147
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">148
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">package</span> main
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">import</span> (
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;context&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;log&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;math/rand&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os/signal&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;strconv&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;sync&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;syscall&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;time&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Message <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	id <span style="color:#458;font-weight:bold">int</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Producer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannels []<span style="color:#000;font-weight:bold">chan</span> Message
</span></span><span style="display:flex;"><span>	ctx          context.Context
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannels []<span style="color:#000;font-weight:bold">chan</span> Message, ctx context.Context) <span style="color:#000;font-weight:bold">*</span>Producer {
</span></span><span style="display:flex;"><span>	p <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Producer{taskChannels: taskChannels,
</span></span><span style="display:flex;"><span>		ctx: ctx}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> p
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (p <span style="color:#000;font-weight:bold">*</span>Producer) <span style="color:#900;font-weight:bold">Produce</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>    random <span style="color:#000;font-weight:bold">:=</span> rand.<span style="color:#900;font-weight:bold">New</span>(rand.<span style="color:#900;font-weight:bold">NewSource</span>(<span style="color:#099">1024</span>))
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#998;font-style:italic">// 判断是否要退出了
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>        <span style="color:#000;font-weight:bold">case</span> <span style="color:#000;font-weight:bold">&lt;-</span>p.ctx.<span style="color:#900;font-weight:bold">Done</span>():
</span></span><span style="display:flex;"><span>            log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s produce break&#34;</span>, name)
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// produce在监听到种终止事件之后关闭channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            <span style="color:#000;font-weight:bold">for</span> _, ch <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> p.taskChannels {
</span></span><span style="display:flex;"><span>                <span style="color:#998;font-style:italic">// 关闭所有持有的channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>                <span style="color:#0086b3">close</span>(ch)
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>            <span style="color:#000;font-weight:bold">return</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">default</span>:
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 模拟生产者获取数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            num <span style="color:#000;font-weight:bold">:=</span> random.<span style="color:#900;font-weight:bold">Int</span>()
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 间隔一秒产出一个数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            time.<span style="color:#900;font-weight:bold">Sleep</span>(<span style="color:#099">1</span> <span style="color:#000;font-weight:bold">*</span> time.Second)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            p.taskChannels[num<span style="color:#000;font-weight:bold">%</span><span style="color:#0086b3">len</span>(p.taskChannels)] <span style="color:#000;font-weight:bold">&lt;-</span> Message{id: num}
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Consumer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannel <span style="color:#000;font-weight:bold">&lt;-</span><span style="color:#000;font-weight:bold">chan</span> Message
</span></span><span style="display:flex;"><span>	wg          <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannel <span style="color:#000;font-weight:bold">&lt;-</span><span style="color:#000;font-weight:bold">chan</span> Message, wg <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup) <span style="color:#000;font-weight:bold">*</span>Consumer {
</span></span><span style="display:flex;"><span>	c <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Consumer{taskChannel: taskChannel,
</span></span><span style="display:flex;"><span>		wg: wg}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> c
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (c <span style="color:#000;font-weight:bold">*</span>Consumer) <span style="color:#900;font-weight:bold">Consume</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">case</span> task, ok <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&lt;-</span>c.taskChannel:
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 模拟消费者处理数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            <span style="color:#000;font-weight:bold">if</span> ok {
</span></span><span style="display:flex;"><span>                log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer  deal: %d&#34;</span>, name, task.id)
</span></span><span style="display:flex;"><span>            } <span style="color:#000;font-weight:bold">else</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#998;font-style:italic">// 数据消费完成，且channel关闭的时候，通知主线程消费者处理管道中的数据完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>                log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer done &#34;</span>, name)
</span></span><span style="display:flex;"><span>                <span style="color:#000;font-weight:bold">goto</span> flag
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#998;font-style:italic">// 跳出多层循环
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>flag:
</span></span><span style="display:flex;"><span>    <span style="color:#998;font-style:italic">// 消费者标识所有消息已经处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>    <span style="color:#000;font-weight:bold">defer</span> c.wg.<span style="color:#900;font-weight:bold">Done</span>()
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (c <span style="color:#000;font-weight:bold">*</span>Consumer) <span style="color:#900;font-weight:bold">GoConsume</span>(name <span style="color:#458;font-weight:bold">string</span>, goNum <span style="color:#458;font-weight:bold">int</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">0</span>; i &lt; goNum; i<span style="color:#000;font-weight:bold">++</span> {
</span></span><span style="display:flex;"><span>		c.wg.<span style="color:#900;font-weight:bold">Add</span>(<span style="color:#099">1</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>(goName <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>			c.<span style="color:#900;font-weight:bold">Consume</span>(goName)
</span></span><span style="display:flex;"><span>		}(name <span style="color:#000;font-weight:bold">+</span> <span style="color:#d14">&#34;-&#34;</span> <span style="color:#000;font-weight:bold">+</span> strconv.<span style="color:#900;font-weight:bold">Itoa</span>(i))
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main start&#34;</span>)
</span></span><span style="display:flex;"><span>	parent <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">Background</span>()
</span></span><span style="display:flex;"><span>	wc, cancelFunc <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">WithCancel</span>(parent)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>() {
</span></span><span style="display:flex;"><span>		signals <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> os.Signal)
</span></span><span style="display:flex;"><span>		signal.<span style="color:#900;font-weight:bold">Notify</span>(signals, syscall.SIGINT, syscall.SIGTERM)
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 这个协程 就阻塞这里等待结果
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		<span style="color:#000;font-weight:bold">&lt;-</span>signals
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 监听进程kill信号，通知produce停止产出
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;start deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#900;font-weight:bold">cancelFunc</span>()
</span></span><span style="display:flex;"><span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;end deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>	}()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 用于判断所有消费者处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>sync.WaitGroup{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	channelNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">3</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	taskChannels <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>([]<span style="color:#000;font-weight:bold">chan</span> Message, channelNum)
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> taskChannels {
</span></span><span style="display:flex;"><span>		taskChannels[i] = <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> Message)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 创建持有多个channel的生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannels, wc)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 创建多个消费者，每个消费者最多持有一个channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumers <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>([]<span style="color:#000;font-weight:bold">*</span>Consumer, <span style="color:#0086b3">len</span>(taskChannels))
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">0</span>; i &lt; <span style="color:#0086b3">len</span>(taskChannels); i<span style="color:#000;font-weight:bold">++</span> {
</span></span><span style="display:flex;"><span>		consumers[i] = <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannels[i], wg)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	<span style="color:#000;font-weight:bold">go</span> producer.<span style="color:#900;font-weight:bold">Produce</span>(<span style="color:#d14">&#34;p&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 每个消费者的的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumerGoNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">1</span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	<span style="color:#000;font-weight:bold">for</span> i, consumer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> consumers {
</span></span><span style="display:flex;"><span>		consumer.<span style="color:#900;font-weight:bold">GoConsume</span>(<span style="color:#d14">&#34;c&#34;</span><span style="color:#000;font-weight:bold">+</span>strconv.<span style="color:#900;font-weight:bold">Itoa</span>(i), consumerGoNum)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 等待consumer完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg.<span style="color:#900;font-weight:bold">Wait</span>()
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main end&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></td></tr></table>
</div>
</div><p>程序运行输出:</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>2023/11/01 11:28:35 main start
</span></span><span style="display:flex;"><span>2023/11/01 11:28:36 c1-0 consumer  deal: 882526541242338811
</span></span><span style="display:flex;"><span>2023/11/01 11:28:37 c2-0 consumer  deal: 6993343009486954757
</span></span><span style="display:flex;"><span>2023/11/01 11:28:38 c1-0 consumer  deal: 3359683908485645107
</span></span><span style="display:flex;"><span>2023/11/01 11:28:39 c0-0 consumer  deal: 5363318930287979427
</span></span><span style="display:flex;"><span>2023/11/01 11:28:40 c2-0 consumer  deal: 7051871835526759457
</span></span><span style="display:flex;"><span>2023/11/01 11:28:41 c1-0 consumer  deal: 6221605816674811576
</span></span><span style="display:flex;"><span>2023/11/01 11:28:41 start deal kill signal
</span></span><span style="display:flex;"><span>2023/11/01 11:28:41 end deal kill signal
</span></span><span style="display:flex;"><span>2023/11/01 11:28:42 p produce break
</span></span><span style="display:flex;"><span>2023/11/01 11:28:42 c0-0 consumer done 
</span></span><span style="display:flex;"><span>2023/11/01 11:28:42 c2-0 consumer done 
</span></span><span style="display:flex;"><span>2023/11/01 11:28:42 c1-0 consumer  deal: 5938020141159581395
</span></span><span style="display:flex;"><span>2023/11/01 11:28:42 c1-0 consumer done 
</span></span><span style="display:flex;"><span>2023/11/01 11:28:42 main end
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="单一生产者多channel单一消费者">单一生产者，多channel，单一消费者</h2>
<p>适用于吞吐量：channel&lt;生产者&lt;消费者 的场景，提升channel的数量提高吞吐量</p>
<h4 id="生产消费模型-3">生产消费模型</h4>
<pre tabindex="0"><code class="language-mermaid" data-lang="mermaid">graph LR;

    生产者1--&gt;channel1;
    生产者1--&gt;channel2;
    生产者1--&gt;channel3;
    channel1--&gt;消费者1;
    channel2--&gt;消费者1;
    channel3--&gt;消费者1;
</code></pre><p>代码实现上，将消费者支持绑定多个channel即可. 当消费者协程绑定的所有channel都close的时候主协程才能退出。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 15
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 16
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 17
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 18
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 19
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 20
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 21
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 22
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 23
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 24
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 25
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 26
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 27
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 28
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 29
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 30
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 31
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 32
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 33
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 34
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 35
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 36
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 37
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 38
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 39
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 40
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 41
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 42
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 43
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 44
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 45
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 46
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 47
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 48
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 49
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 50
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 51
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 52
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 53
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 54
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 55
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 56
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 57
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 58
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 59
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 60
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 61
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 62
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 63
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 64
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 65
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 66
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 67
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 68
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 69
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 70
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 71
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 72
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 73
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 74
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 75
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 76
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 77
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 78
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 79
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 80
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 81
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 82
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 83
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 84
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 85
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 86
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 87
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 88
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 89
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 90
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 91
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 92
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 93
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 94
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 95
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 96
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 97
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 98
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 99
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">100
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">101
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">102
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">103
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">104
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">105
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">106
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">107
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">108
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">109
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">110
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">111
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">112
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">113
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">114
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">115
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">116
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">117
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">118
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">119
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">120
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">121
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">122
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">123
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">124
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">125
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">126
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">127
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">128
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">129
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">130
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">131
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">132
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">133
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">134
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">135
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">136
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">137
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">138
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">139
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">140
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">141
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">142
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">143
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">144
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">145
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">146
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">147
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">148
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">149
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">150
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">151
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">package</span> main
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">import</span> (
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;context&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;log&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;math/rand&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os/signal&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;strconv&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;sync&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;syscall&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;time&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Message <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	id <span style="color:#458;font-weight:bold">int</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Producer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannels []<span style="color:#000;font-weight:bold">chan</span> Message
</span></span><span style="display:flex;"><span>	ctx          context.Context
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannels []<span style="color:#000;font-weight:bold">chan</span> Message, ctx context.Context) <span style="color:#000;font-weight:bold">*</span>Producer {
</span></span><span style="display:flex;"><span>	p <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Producer{taskChannels: taskChannels,
</span></span><span style="display:flex;"><span>		ctx: ctx}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> p
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (p <span style="color:#000;font-weight:bold">*</span>Producer) <span style="color:#900;font-weight:bold">Produce</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>    random <span style="color:#000;font-weight:bold">:=</span> rand.<span style="color:#900;font-weight:bold">New</span>(rand.<span style="color:#900;font-weight:bold">NewSource</span>(<span style="color:#099">1024</span>))
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#998;font-style:italic">// 判断是否要退出了
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>        <span style="color:#000;font-weight:bold">case</span> <span style="color:#000;font-weight:bold">&lt;-</span>p.ctx.<span style="color:#900;font-weight:bold">Done</span>():
</span></span><span style="display:flex;"><span>            log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s produce break&#34;</span>, name)
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// produce在监听到种终止事件之后关闭channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            <span style="color:#000;font-weight:bold">for</span> _, ch <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> p.taskChannels {
</span></span><span style="display:flex;"><span>                <span style="color:#998;font-style:italic">// 关闭所有持有的channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>                <span style="color:#0086b3">close</span>(ch)
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>            <span style="color:#000;font-weight:bold">return</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">default</span>:
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 模拟生产者获取数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            num <span style="color:#000;font-weight:bold">:=</span> random.<span style="color:#900;font-weight:bold">Intn</span>(<span style="color:#0086b3">len</span>(p.taskChannels))
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 间隔一秒产出一个数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            time.<span style="color:#900;font-weight:bold">Sleep</span>(<span style="color:#099">1</span> <span style="color:#000;font-weight:bold">*</span> time.Second)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            p.taskChannels[num] <span style="color:#000;font-weight:bold">&lt;-</span> Message{id: num}
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Consumer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannel []<span style="color:#000;font-weight:bold">chan</span> Message
</span></span><span style="display:flex;"><span>	wg          <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannel []<span style="color:#000;font-weight:bold">chan</span> Message, wg <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup) <span style="color:#000;font-weight:bold">*</span>Consumer {
</span></span><span style="display:flex;"><span>	c <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Consumer{taskChannel: taskChannel,
</span></span><span style="display:flex;"><span>		wg: wg}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> c
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (c <span style="color:#000;font-weight:bold">*</span>Consumer) <span style="color:#900;font-weight:bold">Consume</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>    channelClose <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">map</span>[<span style="color:#458;font-weight:bold">int</span>]<span style="color:#458;font-weight:bold">bool</span>, <span style="color:#0086b3">len</span>(c.taskChannel))
</span></span><span style="display:flex;"><span>    random <span style="color:#000;font-weight:bold">:=</span> rand.<span style="color:#900;font-weight:bold">New</span>(rand.<span style="color:#900;font-weight:bold">NewSource</span>(<span style="color:#099">1024</span>))
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>        index <span style="color:#000;font-weight:bold">:=</span> random.<span style="color:#900;font-weight:bold">Intn</span>(<span style="color:#0086b3">len</span>(c.taskChannel))
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">case</span> task, ok <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&lt;-</span>c.taskChannel[index]:
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 模拟消费者处理数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>            <span style="color:#000;font-weight:bold">if</span> ok {
</span></span><span style="display:flex;"><span>                log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer  deal: %d&#34;</span>, name, task.id)
</span></span><span style="display:flex;"><span>            } <span style="color:#000;font-weight:bold">else</span> {
</span></span><span style="display:flex;"><span>                channelClose[index] = <span style="color:#000;font-weight:bold">true</span>
</span></span><span style="display:flex;"><span>                <span style="color:#000;font-weight:bold">if</span> <span style="color:#0086b3">len</span>(channelClose) <span style="color:#000;font-weight:bold">==</span> <span style="color:#0086b3">len</span>(c.taskChannel) {
</span></span><span style="display:flex;"><span>                    <span style="color:#998;font-style:italic">// 检查所持有的所有channel已经关闭
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>                    <span style="color:#998;font-style:italic">// 数据消费完成，且channel关闭的时候，通知主线程消费者处理管道中的数据完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>                    log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer done &#34;</span>, name)
</span></span><span style="display:flex;"><span>                    <span style="color:#000;font-weight:bold">goto</span> flag
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">default</span>:
</span></span><span style="display:flex;"><span>            <span style="color:#998;font-style:italic">// 当存在多个channel的时候，避免等待一个一直阻塞，尝试获取其他channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#998;font-style:italic">// 跳出多层循环
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>flag:
</span></span><span style="display:flex;"><span>    <span style="color:#998;font-style:italic">// 消费者标识所有消息已经处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>    <span style="color:#000;font-weight:bold">defer</span> c.wg.<span style="color:#900;font-weight:bold">Done</span>()
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (c <span style="color:#000;font-weight:bold">*</span>Consumer) <span style="color:#900;font-weight:bold">GoConsume</span>(name <span style="color:#458;font-weight:bold">string</span>, goNum <span style="color:#458;font-weight:bold">int</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">0</span>; i &lt; goNum; i<span style="color:#000;font-weight:bold">++</span> {
</span></span><span style="display:flex;"><span>		c.wg.<span style="color:#900;font-weight:bold">Add</span>(<span style="color:#099">1</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>(goName <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>			c.<span style="color:#900;font-weight:bold">Consume</span>(goName)
</span></span><span style="display:flex;"><span>		}(name <span style="color:#000;font-weight:bold">+</span> <span style="color:#d14">&#34;-&#34;</span> <span style="color:#000;font-weight:bold">+</span> strconv.<span style="color:#900;font-weight:bold">Itoa</span>(i))
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main start&#34;</span>)
</span></span><span style="display:flex;"><span>	parent <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">Background</span>()
</span></span><span style="display:flex;"><span>	wc, cancelFunc <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">WithCancel</span>(parent)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>() {
</span></span><span style="display:flex;"><span>		signals <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> os.Signal)
</span></span><span style="display:flex;"><span>		signal.<span style="color:#900;font-weight:bold">Notify</span>(signals, syscall.SIGINT, syscall.SIGTERM)
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 这个协程 就阻塞这里等待结果
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		<span style="color:#000;font-weight:bold">&lt;-</span>signals
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 监听进程kill信号，通知produce停止产出
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;start deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#900;font-weight:bold">cancelFunc</span>()
</span></span><span style="display:flex;"><span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;end deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>	}()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 用于判断所有消费者处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>sync.WaitGroup{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	channelNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">3</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	taskChannels <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>([]<span style="color:#000;font-weight:bold">chan</span> Message, channelNum)
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> taskChannels {
</span></span><span style="display:flex;"><span>		taskChannels[i] = <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> Message)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 创建持有多个channel的生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannels, wc)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 创建持有多个channel的消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannels, wg)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	<span style="color:#000;font-weight:bold">go</span> producer.<span style="color:#900;font-weight:bold">Produce</span>(<span style="color:#d14">&#34;p&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 消费者的的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumerGoNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">1</span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumer.<span style="color:#900;font-weight:bold">GoConsume</span>(<span style="color:#d14">&#34;c&#34;</span>, consumerGoNum)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 等待consumer完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg.<span style="color:#900;font-weight:bold">Wait</span>()
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main end&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></td></tr></table>
</div>
</div><p>程序运行输出:</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>2023/11/01 12:23:58 main start
</span></span><span style="display:flex;"><span>2023/11/01 12:23:59 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 12:24:00 c-0 consumer  deal: 1
</span></span><span style="display:flex;"><span>2023/11/01 12:24:01 c-0 consumer  deal: 2
</span></span><span style="display:flex;"><span>2023/11/01 12:24:02 c-0 consumer  deal: 2
</span></span><span style="display:flex;"><span>2023/11/01 12:24:03 start deal kill signal
</span></span><span style="display:flex;"><span>2023/11/01 12:24:03 end deal kill signal
</span></span><span style="display:flex;"><span>2023/11/01 12:24:03 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 12:24:03 p produce break
</span></span><span style="display:flex;"><span>2023/11/01 12:24:03 c-0 consumer done 
</span></span><span style="display:flex;"><span>2023/11/01 12:24:03 main end
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="多生产者单channel单消费者">多生产者，单channel，单消费者</h2>
<p>适用于吞吐量排序：生产者&lt; &lt;消费者&lt;channel 生产者的数量可以提高吞吐量的同时避免channel和消费者的浪费</p>
<h4 id="生产消费模型-4">生产消费模型</h4>
<pre tabindex="0"><code class="language-mermaid" data-lang="mermaid">graph LR;

    生产者1--&gt;channel;
    生产者2--&gt;channel;
    生产者3--&gt;channel;
    生产者4--&gt;channel;
    channel--&gt;消费者1;
    channel--&gt;消费者1;
    channel--&gt;消费者1;
</code></pre><p>将代码支持多生产者创建即可，需要注意的是go进程接受到kill信号之后，要确保所有的生产者都接受到了cancel信号停止生产后再关闭channel避免消息的丢失和
生产者向已经关闭的channel中发送消息的情况出现</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 15
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 16
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 17
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 18
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 19
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 20
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 21
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 22
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 23
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 24
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 25
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 26
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 27
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 28
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 29
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 30
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 31
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 32
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 33
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 34
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 35
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 36
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 37
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 38
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 39
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 40
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 41
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 42
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 43
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 44
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 45
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 46
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 47
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 48
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 49
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 50
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 51
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 52
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 53
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 54
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 55
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 56
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 57
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 58
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 59
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 60
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 61
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 62
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 63
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 64
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 65
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 66
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 67
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 68
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 69
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 70
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 71
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 72
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 73
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 74
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 75
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 76
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 77
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 78
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 79
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 80
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 81
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 82
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 83
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 84
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 85
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 86
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 87
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 88
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 89
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 90
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 91
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 92
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 93
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 94
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 95
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 96
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 97
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 98
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 99
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">100
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">101
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">102
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">103
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">104
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">105
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">106
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">107
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">108
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">109
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">110
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">111
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">112
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">113
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">114
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">115
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">116
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">117
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">118
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">119
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">120
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">121
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">122
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">123
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">124
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">125
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">126
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">127
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">128
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">129
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">130
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">131
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">132
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">133
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">134
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">135
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">136
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">137
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">138
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">139
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">140
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">141
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">142
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">143
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">144
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">145
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">146
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">147
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">148
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">149
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">150
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">151
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">152
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">153
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">154
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">155
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">156
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">157
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">158
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">159
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">160
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">161
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">162
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">163
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">164
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">165
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">166
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">167
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">168
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">169
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">170
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">171
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">172
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">173
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">174
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">175
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">176
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">177
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">178
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">179
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">180
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">181
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">182
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">package</span> main
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">import</span> (
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;context&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;log&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;math/rand&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;os/signal&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;strconv&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;sync&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;syscall&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#d14">&#34;time&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Message <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	id <span style="color:#458;font-weight:bold">int</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Producer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannels []<span style="color:#000;font-weight:bold">chan</span> Message
</span></span><span style="display:flex;"><span>	ctx          context.Context
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 用于等待所有的生产者全部接受到停止信号停止处理
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg           <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 所有生产者接受到停止信号的时候，只有一个生产者去关闭所有taskChannels
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	once         <span style="color:#000;font-weight:bold">*</span>sync.Once
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannels []<span style="color:#000;font-weight:bold">chan</span> Message, ctx context.Context) <span style="color:#000;font-weight:bold">*</span>Producer {
</span></span><span style="display:flex;"><span>	p <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Producer{taskChannels: taskChannels,
</span></span><span style="display:flex;"><span>		ctx:  ctx,
</span></span><span style="display:flex;"><span>		wg:   <span style="color:#000;font-weight:bold">&amp;</span>sync.WaitGroup{},
</span></span><span style="display:flex;"><span>		once: <span style="color:#000;font-weight:bold">&amp;</span>sync.Once{}}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> p
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (p <span style="color:#000;font-weight:bold">*</span>Producer) <span style="color:#900;font-weight:bold">Produce</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>	random <span style="color:#000;font-weight:bold">:=</span> rand.<span style="color:#900;font-weight:bold">New</span>(rand.<span style="color:#900;font-weight:bold">NewSource</span>(<span style="color:#099">1024</span>))
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 判断是否要退出了
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		<span style="color:#000;font-weight:bold">case</span> <span style="color:#000;font-weight:bold">&lt;-</span>p.ctx.<span style="color:#900;font-weight:bold">Done</span>():
</span></span><span style="display:flex;"><span>			log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s produce break start&#34;</span>, name)
</span></span><span style="display:flex;"><span>			<span style="color:#998;font-style:italic">// 通知通知监听者组任务的协程我受到取消信号了
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>			p.wg.<span style="color:#900;font-weight:bold">Done</span>()
</span></span><span style="display:flex;"><span>			<span style="color:#998;font-style:italic">// 等待所有协程都处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>			p.wg.<span style="color:#900;font-weight:bold">Wait</span>()
</span></span><span style="display:flex;"><span>			log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s produce break done&#34;</span>, name)
</span></span><span style="display:flex;"><span>			p.once.<span style="color:#900;font-weight:bold">Do</span>(<span style="color:#000;font-weight:bold">func</span>() {
</span></span><span style="display:flex;"><span>				<span style="color:#998;font-style:italic">// produce在监听到种终止事件之后关闭channel，所有的produce只执行一次
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>				<span style="color:#000;font-weight:bold">for</span> _, ch <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> p.taskChannels {
</span></span><span style="display:flex;"><span>					<span style="color:#998;font-style:italic">// 关闭所有持有的channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>					<span style="color:#0086b3">close</span>(ch)
</span></span><span style="display:flex;"><span>				}
</span></span><span style="display:flex;"><span>			})
</span></span><span style="display:flex;"><span>			<span style="color:#998;font-style:italic">// 这个之后的代码是否能执行到就不一定了，因为channel关闭就可能导致主协程wait终止掉
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>
</span></span><span style="display:flex;"><span>			<span style="color:#000;font-weight:bold">return</span>
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">default</span>:
</span></span><span style="display:flex;"><span>			<span style="color:#998;font-style:italic">// 模拟生产者获取数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>			num <span style="color:#000;font-weight:bold">:=</span> random.<span style="color:#900;font-weight:bold">Intn</span>(<span style="color:#0086b3">len</span>(p.taskChannels))
</span></span><span style="display:flex;"><span>			<span style="color:#998;font-style:italic">// 间隔一秒产出一个数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>			time.<span style="color:#900;font-weight:bold">Sleep</span>(<span style="color:#099">1</span> <span style="color:#000;font-weight:bold">*</span> time.Second)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>			p.taskChannels[num] <span style="color:#000;font-weight:bold">&lt;-</span> Message{id: num}
</span></span><span style="display:flex;"><span>		}
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (p <span style="color:#000;font-weight:bold">*</span>Producer) <span style="color:#900;font-weight:bold">GoProduce</span>(name <span style="color:#458;font-weight:bold">string</span>, goNum <span style="color:#458;font-weight:bold">int</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">0</span>; i &lt; goNum; i<span style="color:#000;font-weight:bold">++</span> {
</span></span><span style="display:flex;"><span>		p.wg.<span style="color:#900;font-weight:bold">Add</span>(<span style="color:#099">1</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>(goName <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>			p.<span style="color:#900;font-weight:bold">Produce</span>(goName)
</span></span><span style="display:flex;"><span>		}(name <span style="color:#000;font-weight:bold">+</span> <span style="color:#d14">&#34;-&#34;</span> <span style="color:#000;font-weight:bold">+</span> strconv.<span style="color:#900;font-weight:bold">Itoa</span>(i))
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">/*
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">   消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">type</span> Consumer <span style="color:#000;font-weight:bold">struct</span> {
</span></span><span style="display:flex;"><span>	taskChannel []<span style="color:#000;font-weight:bold">chan</span> Message
</span></span><span style="display:flex;"><span>	wg          <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannel []<span style="color:#000;font-weight:bold">chan</span> Message, wg <span style="color:#000;font-weight:bold">*</span>sync.WaitGroup) <span style="color:#000;font-weight:bold">*</span>Consumer {
</span></span><span style="display:flex;"><span>	c <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>Consumer{taskChannel: taskChannel,
</span></span><span style="display:flex;"><span>		wg: wg}
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">return</span> c
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (c <span style="color:#000;font-weight:bold">*</span>Consumer) <span style="color:#900;font-weight:bold">Consume</span>(name <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>	channelClose <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">map</span>[<span style="color:#458;font-weight:bold">int</span>]<span style="color:#458;font-weight:bold">bool</span>, <span style="color:#0086b3">len</span>(c.taskChannel))
</span></span><span style="display:flex;"><span>	random <span style="color:#000;font-weight:bold">:=</span> rand.<span style="color:#900;font-weight:bold">New</span>(rand.<span style="color:#900;font-weight:bold">NewSource</span>(<span style="color:#099">1024</span>))
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> {
</span></span><span style="display:flex;"><span>		index <span style="color:#000;font-weight:bold">:=</span> random.<span style="color:#900;font-weight:bold">Intn</span>(<span style="color:#0086b3">len</span>(c.taskChannel))
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">select</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">case</span> task, ok <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&lt;-</span>c.taskChannel[index]:
</span></span><span style="display:flex;"><span>			<span style="color:#998;font-style:italic">// 模拟消费者处理数据
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>			<span style="color:#000;font-weight:bold">if</span> ok {
</span></span><span style="display:flex;"><span>				log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer  deal: %d&#34;</span>, name, task.id)
</span></span><span style="display:flex;"><span>			} <span style="color:#000;font-weight:bold">else</span> {
</span></span><span style="display:flex;"><span>				<span style="color:#998;font-style:italic">// 每个生产者都去确认自己所消费的channel都已经关闭
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>				channelClose[index] = <span style="color:#000;font-weight:bold">true</span>
</span></span><span style="display:flex;"><span>				<span style="color:#000;font-weight:bold">if</span> <span style="color:#0086b3">len</span>(channelClose) <span style="color:#000;font-weight:bold">==</span> <span style="color:#0086b3">len</span>(c.taskChannel) {
</span></span><span style="display:flex;"><span>					<span style="color:#998;font-style:italic">// 检查所持有的所有channel已经关闭
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>					<span style="color:#998;font-style:italic">// 数据消费完成，且channel关闭的时候，通知主线程消费者处理管道中的数据完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>					log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;%s consumer done &#34;</span>, name)
</span></span><span style="display:flex;"><span>					<span style="color:#000;font-weight:bold">goto</span> flag
</span></span><span style="display:flex;"><span>				}
</span></span><span style="display:flex;"><span>			}
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">default</span>:
</span></span><span style="display:flex;"><span>			<span style="color:#998;font-style:italic">// 当存在多个channel的时候，避免等待一个一直阻塞，尝试获取其他channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		}
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 跳出多层循环
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>flag:
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 消费者标识所有消息已经处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	<span style="color:#000;font-weight:bold">defer</span> c.wg.<span style="color:#900;font-weight:bold">Done</span>()
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> (c <span style="color:#000;font-weight:bold">*</span>Consumer) <span style="color:#900;font-weight:bold">GoConsume</span>(name <span style="color:#458;font-weight:bold">string</span>, goNum <span style="color:#458;font-weight:bold">int</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">0</span>; i &lt; goNum; i<span style="color:#000;font-weight:bold">++</span> {
</span></span><span style="display:flex;"><span>		c.wg.<span style="color:#900;font-weight:bold">Add</span>(<span style="color:#099">1</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>(goName <span style="color:#458;font-weight:bold">string</span>) {
</span></span><span style="display:flex;"><span>			c.<span style="color:#900;font-weight:bold">Consume</span>(goName)
</span></span><span style="display:flex;"><span>		}(name <span style="color:#000;font-weight:bold">+</span> <span style="color:#d14">&#34;-&#34;</span> <span style="color:#000;font-weight:bold">+</span> strconv.<span style="color:#900;font-weight:bold">Itoa</span>(i))
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main start&#34;</span>)
</span></span><span style="display:flex;"><span>	parent <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">Background</span>()
</span></span><span style="display:flex;"><span>	wc, cancelFunc <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">WithCancel</span>(parent)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>() {
</span></span><span style="display:flex;"><span>		signals <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> os.Signal)
</span></span><span style="display:flex;"><span>		signal.<span style="color:#900;font-weight:bold">Notify</span>(signals, syscall.SIGINT, syscall.SIGTERM)
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 这个协程 就阻塞这里等待结果
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		<span style="color:#000;font-weight:bold">&lt;-</span>signals
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 监听进程kill信号，通知produce停止产出
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;start deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#900;font-weight:bold">cancelFunc</span>()
</span></span><span style="display:flex;"><span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;end deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>	}()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 用于判断所有消费者处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>sync.WaitGroup{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 生产者的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producerNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">4</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 管道的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	channelNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">1</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 消费者的的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumerGoNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">1</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// channel创建
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	taskChannels <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>([]<span style="color:#000;font-weight:bold">chan</span> Message, channelNum)
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> taskChannels {
</span></span><span style="display:flex;"><span>		taskChannels[i] = <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> Message)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 生产者创建
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannels, wc)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 消费者创建
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannels, wg)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producer.<span style="color:#900;font-weight:bold">GoProduce</span>(<span style="color:#d14">&#34;p&#34;</span>, producerNum)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumer.<span style="color:#900;font-weight:bold">GoConsume</span>(<span style="color:#d14">&#34;c&#34;</span>, consumerGoNum)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 等待consumer完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg.<span style="color:#900;font-weight:bold">Wait</span>()
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main end&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></td></tr></table>
</div>
</div><p>程序的运行日志</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">25
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">26
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">27
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">28
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">29
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>2023/11/01 13:35:32 main start
</span></span><span style="display:flex;"><span>2023/11/01 13:35:33 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:33 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:33 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:33 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:34 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:34 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:34 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:34 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:35 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:35 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:35 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:35 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 start deal kill signal
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 end deal kill signal
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 p-0 produce break start
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 c-0 consumer  deal: 0
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 p-2 produce break start
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 p-1 produce break start
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 p-3 produce break start
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 p-3 produce break done
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 c-0 consumer done 
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 p-2 produce break done
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 p-1 produce break done
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 p-0 produce break done
</span></span><span style="display:flex;"><span>2023/11/01 13:35:36 main end
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="多生产者单channel多消费者">多生产者，单channel，多消费者</h2>
<p>适用于吞吐量排序：生产者&lt;消费者&lt; &lt;channel 或 消费者&lt;生产者&lt; &lt;channel 这时候提高消费者和生产者的数量保证消费平衡，且减少channel资源的浪费</p>
<h4 id="生产消费模型-5">生产消费模型</h4>
<pre tabindex="0"><code class="language-mermaid" data-lang="mermaid">graph LR;

    生产者1--&gt;channel;
    生产者2--&gt;channel;
    生产者3--&gt;channel;
    生产者4--&gt;channel;
    channel--&gt;消费者1;
    channel--&gt;消费者2;
    channel--&gt;消费者3;
</code></pre><p>代码只需要将上述章节中的 consumerGoNum 变量改成3即可。</p>
<h2 id="多生产者多channel单一消费者">多生产者，多channel，单一消费者</h2>
<p>适用于吞吐量排序: 生产者&lt;channel&lt; &lt; 消费者</p>
<h4 id="生产消费模型-6">生产消费模型</h4>
<pre tabindex="0"><code class="language-mermaid" data-lang="mermaid">graph LR;

    生产者1--&gt;channel1;
    生产者2--&gt;channel2;
    生产者3--&gt;channel3;
    channel1--&gt;消费者1;
    channel2--&gt;消费者1;
    channel3--&gt;消费者1;
</code></pre><p>不需要修改consumer和producer的代码，只需要修改main方法中编排生产者和消费者的逻辑即可。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">25
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">26
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">27
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">28
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">29
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">30
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">31
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">32
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">33
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">34
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">35
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">36
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">37
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">38
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">39
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">40
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">41
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">42
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">43
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">44
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">45
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">46
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">47
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">48
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">49
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">50
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">51
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">52
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">53
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">54
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">55
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">56
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">57
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">58
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">59
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">60
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">61
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main start&#34;</span>)
</span></span><span style="display:flex;"><span>	parent <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">Background</span>()
</span></span><span style="display:flex;"><span>	wc, cancelFunc <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">WithCancel</span>(parent)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>() {
</span></span><span style="display:flex;"><span>		signals <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> os.Signal)
</span></span><span style="display:flex;"><span>		signal.<span style="color:#900;font-weight:bold">Notify</span>(signals, syscall.SIGINT, syscall.SIGTERM)
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 这个协程 就阻塞这里等待结果
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		<span style="color:#000;font-weight:bold">&lt;-</span>signals
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 监听进程kill信号，通知produce停止产出
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;start deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#900;font-weight:bold">cancelFunc</span>()
</span></span><span style="display:flex;"><span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;end deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>	}()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 用于判断所有消费者处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>sync.WaitGroup{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 生产者的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producerGoNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">1</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 管道的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	channelNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">1</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 消费者的的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumerGoNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">1</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// new生产者的个数
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	newProducerNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">3</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 创建生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producers <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>([]<span style="color:#000;font-weight:bold">*</span>Producer, newProducerNum)
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// all task channel
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	<span style="color:#000;font-weight:bold">var</span> allTaskChannel []<span style="color:#000;font-weight:bold">chan</span> Message
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">0</span>; i &lt; newProducerNum; i<span style="color:#000;font-weight:bold">++</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// channel创建
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		taskChannels <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>([]<span style="color:#000;font-weight:bold">chan</span> Message, channelNum)
</span></span><span style="display:flex;"><span>		<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> taskChannels {
</span></span><span style="display:flex;"><span>			taskChannels[i] = <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> Message)
</span></span><span style="display:flex;"><span>			allTaskChannel = <span style="color:#0086b3">append</span>(allTaskChannel, taskChannels[i])
</span></span><span style="display:flex;"><span>		}
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 生产者创建
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		producers[i] = <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannels, wc)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 消费者创建
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(allTaskChannel, wg)
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumer.<span style="color:#900;font-weight:bold">GoConsume</span>(<span style="color:#d14">&#34;c&#34;</span>, consumerGoNum)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	<span style="color:#000;font-weight:bold">for</span> i, producer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> producers {
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 启动生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		producer.<span style="color:#900;font-weight:bold">GoProduce</span>(<span style="color:#d14">&#34;p&#34;</span><span style="color:#000;font-weight:bold">+</span>strconv.<span style="color:#900;font-weight:bold">Itoa</span>(i), producerGoNum)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 等待consumer完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg.<span style="color:#900;font-weight:bold">Wait</span>()
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main end&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="多生产者多channel多消费者">多生产者，多channel，多消费者</h2>
<p>适用于生产者、消费者、channel之间需要按照特殊的比例配置才能达到吞吐能力平衡的状态，较少使用。因为大部分场景只需要保证所有消费者和channel的吞吐量
高于所有生产者即可，甚至还需要一些消费能力的冗余,因此用其它生产消费模型也能实现。</p>
<h4 id="生产消费模型-7">生产消费模型</h4>
<pre tabindex="0"><code class="language-mermaid" data-lang="mermaid">graph LR;

    生产者1--&gt;channel1;
    生产者1--&gt;channel2;
    生产者2--&gt;channel1;
    生产者2--&gt;channel2;
    channel1--&gt;消费者1;
    channel1--&gt;消费者2;
    channel2--&gt;消费者1;
    channel2--&gt;消费者2;
</code></pre><p>代码实现只需要修改如下变量即可</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span>producerNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">2</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">// 管道的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>channelNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">2</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">// 消费者的的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>consumerGoNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">2</span>
</span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">25
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">26
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">27
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">28
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">29
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">30
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">31
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">32
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">33
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">34
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">35
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">36
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">37
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">38
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">39
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">40
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">41
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">42
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">43
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">44
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">45
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">46
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">47
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">48
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">49
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">func</span> <span style="color:#900;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main start&#34;</span>)
</span></span><span style="display:flex;"><span>	parent <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">Background</span>()
</span></span><span style="display:flex;"><span>	wc, cancelFunc <span style="color:#000;font-weight:bold">:=</span> context.<span style="color:#900;font-weight:bold">WithCancel</span>(parent)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">go</span> <span style="color:#000;font-weight:bold">func</span>() {
</span></span><span style="display:flex;"><span>		signals <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> os.Signal)
</span></span><span style="display:flex;"><span>		signal.<span style="color:#900;font-weight:bold">Notify</span>(signals, syscall.SIGINT, syscall.SIGTERM)
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 这个协程 就阻塞这里等待结果
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		<span style="color:#000;font-weight:bold">&lt;-</span>signals
</span></span><span style="display:flex;"><span>		<span style="color:#998;font-style:italic">// 监听进程kill信号，通知produce停止产出
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;start deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#900;font-weight:bold">cancelFunc</span>()
</span></span><span style="display:flex;"><span>		log.<span style="color:#900;font-weight:bold">Printf</span>(<span style="color:#d14">&#34;end deal kill signal&#34;</span>)
</span></span><span style="display:flex;"><span>	}()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 用于判断所有消费者处理完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">&amp;</span>sync.WaitGroup{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 生产者的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producerNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">2</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 管道的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	channelNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">2</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 消费者的的数量
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumerGoNum <span style="color:#000;font-weight:bold">:=</span> <span style="color:#099">2</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// channel创建
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	taskChannels <span style="color:#000;font-weight:bold">:=</span> <span style="color:#0086b3">make</span>([]<span style="color:#000;font-weight:bold">chan</span> Message, channelNum)
</span></span><span style="display:flex;"><span>	<span style="color:#000;font-weight:bold">for</span> i <span style="color:#000;font-weight:bold">:=</span> <span style="color:#000;font-weight:bold">range</span> taskChannels {
</span></span><span style="display:flex;"><span>		taskChannels[i] = <span style="color:#0086b3">make</span>(<span style="color:#000;font-weight:bold">chan</span> Message)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 生产者创建
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewProducer</span>(taskChannels, wc)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 消费者创建
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumer <span style="color:#000;font-weight:bold">:=</span> <span style="color:#900;font-weight:bold">NewConsumer</span>(taskChannels, wg)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动生产者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	producer.<span style="color:#900;font-weight:bold">GoProduce</span>(<span style="color:#d14">&#34;p&#34;</span>, producerNum)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 启动消费者
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	consumer.<span style="color:#900;font-weight:bold">GoConsume</span>(<span style="color:#d14">&#34;c&#34;</span>, consumerGoNum)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#998;font-style:italic">// 等待consumer完成
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic"></span>	wg.<span style="color:#900;font-weight:bold">Wait</span>()
</span></span><span style="display:flex;"><span>	log.<span style="color:#900;font-weight:bold">Print</span>(<span style="color:#d14">&#34;main end&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></td></tr></table>
</div>
</div>
        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="http://kklt1996.gitee.io/blog/">lsy</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="http://kklt1996.gitee.io/blog/post/golang/2023-10-31-go%E7%94%9F%E4%BA%A7%E6%B6%88%E8%B4%B9%E6%A8%A1%E5%9E%8B/">http://kklt1996.gitee.io/blog/post/golang/2023-10-31-go%E7%94%9F%E4%BA%A7%E6%B6%88%E8%B4%B9%E6%A8%A1%E5%9E%8B/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/blog/post/golang/2023-10-31-go%E5%8D%8F%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1/">golang协程间通信</a></li>
        
        <li><a href="/blog/post/java/2020-05-26-%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F/">单利模式发布对象</a></li>
        
        <li><a href="/blog/post/redis/2020-05-20-redis%E5%AE%9E%E7%8E%B0%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/">Redis实现分布式锁</a></li>
        
        <li><a href="/blog/post/java/2020-05-04-%E5%8E%9F%E5%AD%90%E6%80%A7-%E5%8F%AF%E8%A7%81%E6%80%A7-%E6%9C%89%E5%BA%8F%E6%80%A7/">原子性 可见性 有序性</a></li>
        
        <li><a href="/blog/post/java/2020-04-21-juc-%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E7%B1%BB/">线程安全的类</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            没有标签
            
        </div>
    </article>
    
    

    
    
</div>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='http://kklt1996.gitee.io/blog/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="http://kklt1996.gitee.io/blog/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/golang/2023-10-31-go%E7%94%9F%E4%BA%A7%E6%B6%88%E8%B4%B9%E6%A8%A1%E5%9E%8B/" title="golang中的生产消费模型">golang中的生产消费模型</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/golang/2023-10-31-go%E5%8D%8F%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1/" title="golang协程间通信">golang协程间通信</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-05-26-%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F/" title="单利模式发布对象">单利模式发布对象</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/redis/2020-05-20-redis%E5%AE%9E%E7%8E%B0%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/" title="Redis实现分布式锁">Redis实现分布式锁</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-05-04-%E5%8E%9F%E5%AD%90%E6%80%A7-%E5%8F%AF%E8%A7%81%E6%80%A7-%E6%9C%89%E5%BA%8F%E6%80%A7/" title="原子性 可见性 有序性">原子性 可见性 有序性</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-04-21-juc-%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E7%B1%BB/" title="线程安全的类">线程安全的类</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-04-21-java%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B/" title="java内存模型">java内存模型</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/mybatis/2020-04-12-mybatis%E6%89%B9%E9%87%8F%E6%8F%92%E5%85%A5%E8%BF%94%E5%9B%9E%E8%87%AA%E5%A2%9E%E4%B8%BB%E9%94%AE/" title="mybatis批量插入返回自增主键">mybatis批量插入返回自增主键</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-04-14-juc-%E7%BA%BF%E7%A8%8B%E6%B1%A0/" title="java中的线程池">java中的线程池</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-04-14-juc-%E4%BF%A1%E5%8F%B7%E9%87%8F/" title="Semaphore信号量">Semaphore信号量</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title">分类</h3>
<ul class="widget-list">
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/elasticsearch/">elasticsearch (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/golang/">golang (2)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/java%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/">java并发编程 (6)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/json/">json (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/mybatis/">mybatis (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/mysql/">mysql (3)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/redis/">redis (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/springBoot/">springBoot (11)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/ubuntu/">ubuntu (3)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/">数据结构和算法 (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习 (1)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title">标签</h3>
<div class="tagcloud">
    
    <a href="http://kklt1996.gitee.io/blog/tags/aspect/">aspect</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/devTool/">devTool</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/docker/">docker</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/filter/">filter</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/hibernate/">hibernate</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/idea/">idea</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/intercept/">intercept</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/interceptor/">interceptor</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/jackjson/">jackjson</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/resultful/">resultful</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/springTest/">springTest</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/spring%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/">spring自定义异常处理</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/swagger/">swagger</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/swap/">swap</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/validator/">validator</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/%E5%8A%A8%E6%80%81%E6%95%B0%E6%8D%AE%E6%BA%90/">动态数据源</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/%E9%98%B2%E7%81%AB%E5%A2%99/">防火墙</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="https://gitee.com/kklt1996" title="码云地址">码云地址</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="http://kklt1996.gitee.io/blog/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>

    
    <script
            src="https://cdn.jsdelivr.net/npm/mermaid@8.8.2/dist/mermaid.min.js"
            integrity="sha256-KqisLh8jVMBRjpNkOhH5W9VWs+F6x6vQksLqxs7+x9A="
            crossorigin="anonymous"
    ></script>
    <script>
        
        Array.from(document.getElementsByClassName("language-mermaid")).forEach(
            (el) => {
                el.parentElement.outerHTML = `<div class="mermaid">${el.innerText}</div>`;
            }
        );
    </script>
    <style>
         
        .mermaid svg {
            display: block;
            margin: auto;
        }
    </style>
    

    <footer id="footer">
    <div class="container">
        &copy; 2023 <a href="http://kklt1996.gitee.io/blog/">lsy&#39;blog By lsy</a>.
        Powered by <a rel="nofollow noreferer noopener" href="https://gohugo.io" target="_blank">Hugo</a>.
        <a href="https://www.flysnow.org/" target="_blank">Theme</a> based on <a href="https://github.com/flysnow-org/maupassant-hugo" target="_blank">maupassant</a>.
        
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script>


<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/blog/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/js/douban.js'></script>

</body>

</html>